//
//  SDAnswerTestReportDetailVC.m
//  skinDetection
//
//  Created by  liangzhun on 2019/3/6.
//  Copyright © 2019年  liangzhun. All rights reserved.
//

#import "SDAnswerTestReportDetailVC.h"
#import "SDAnswerDetectionHeaderView.h"

#import "SDAnswerTestHeaderView.h"

#import "SDProductDetailVC.h"

#import "SDAnswerTestResultCell.h"
#import "SDAnswerTestResultProductCell.h"
#import "SDAnswerTestResultProductCell2.h"
#import "SDProductListCell.h"

#import "SDTestReportDeatilData.h"
#import "SDTestReportDeatilDetailList.h"
#import "SDTestReportDeatilProductList.h"
#import "SDTestReportDeatilBaseClass.h"

#import "SDAnswersDetectionModel.h"

static NSString *const cellIden = @"SDAnswerTestResultProductCell";
static NSString *const cellIden1 = @"SDAnswerTestResultCell";
static NSString *const cellIden2 = @"SDAnswerTestResultProductCell2";
static NSString *const headerIden = @"SDAnswerTestHeaderView";
static NSString *const cellIden3 = @"SDProductListCell";


@interface SDAnswerTestReportDetailVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIView *headerView;

@property (weak, nonatomic) IBOutlet UIView *layerView;

@property (weak, nonatomic) IBOutlet UIView *LabView;

@property (weak, nonatomic) IBOutlet UILabel *topLab;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic ,strong) SDAnswerDetectionHeaderView *scrHeaderView;

@property (nonatomic ,strong) SDTestReportDeatilData *dataReport;

@property (nonatomic ,strong) NSIndexPath *indexPath;
@end

@implementation SDAnswerTestReportDetailVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navHeight.constant = SDNavBar_Height;
    [self layOut];
    [self setViews];
    [self loadData];
   
}
- (void)loadData
{
    [[SDAnswersDetectionModel shareInstance] loadDataWithBlock:^(BOOL success) {
        
        [self.scrHeaderView.collectionView reloadData];
        [self getDetailReport];
        
    }];
    
}
- (void)getDetailReport
{
    [SDNetwork getQuestionReportDetailWithID:[self.data.id intValue] Finish:^(NSDictionary * _Nullable dic, NSError * _Nullable error) {
        if (!error) {
          
            SDTestReportDeatilBaseClass *class = [SDTestReportDeatilBaseClass modelObjectWithDictionary:dic];
            self.dataReport = class.data;
             self.indexPath = self.scrHeaderView.indexpath;
            
        }
    }];
}
- (void)setViews
{
     self.LabView.layer.shadowColor = rgba(0, 0, 0, 0.07).CGColor;
     [self.collectionView registerNib:[UINib nibWithNibName:cellIden bundle:nil] forCellWithReuseIdentifier:cellIden];
     [self.collectionView registerNib:[UINib nibWithNibName:cellIden2 bundle:nil] forCellWithReuseIdentifier:cellIden2];
     [self.collectionView registerNib:[UINib nibWithNibName:cellIden1 bundle:nil] forCellWithReuseIdentifier:cellIden1];
    [self.collectionView registerNib:[UINib nibWithNibName:cellIden3 bundle:nil] forCellWithReuseIdentifier:cellIden3];
    
    [self.collectionView registerNib:[UINib nibWithNibName:headerIden bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIden];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
     SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];
    if (section == 0) return list.resultDesc.length == 0? 0:1;
    
    return list.productList.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        SDAnswerTestResultCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden1 forIndexPath:indexPath];
        
      SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];
      [cell.contentLab setAttributedText:nil];
        if (list.resultFeature.length > 0)
        {

            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = 5.0f;
            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:list.resultFeature attributes:@{NSParagraphStyleAttributeName:style}];
            [cell.contentLab setAttributedText:att];

        }
        
        return cell;
        
    }else{
     
        SDProductListCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIden3 forIndexPath:indexPath];
         SDTestReportDeatilDetailList *deatilList = self.dataReport.detailList[self.indexPath.row];
        SDTestReportDeatilProductList *productList = deatilList.productList[indexPath.row];
        [cell.productImage sd_setImageWithURL:[NSURL URLWithString:productList.sImg]];
        [cell.productTitle setText:productList.productName];
        return cell;
        
    }
    
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    SDAnswerTestHeaderView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerIden forIndexPath:indexPath];
    header.titleLab.text = indexPath.section == 0? @"特征":@"推荐产品";
    SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];
    
    if (indexPath.section == 0) {
        
        header.hidden = list.resultFeature.length > 0? NO:YES;
        
    }else{
        header.hidden = list.productList.count > 0? NO:YES;
    }
    return header;
    
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    if (indexPath.section == 0) {
        
       SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];

        CGFloat height = 0 ;
        if (list.resultFeature.length > 0)
        {

            NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
            style.lineSpacing = 5.f;
            NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:list.resultFeature attributes:@{NSParagraphStyleAttributeName:style}];

            height = [SDMethodTool getAttTextHeightWithText:att withTextFontSize:Font(15) Width:SDMainScreenWidth - 30] + 2;
        }
        
        return CGSizeMake(SDMainScreenWidth, height + 40);
        
    }
    
    return CGSizeMake((SDMainScreenWidth - 45)/2.0, 78 + (SDMainScreenWidth - 45 - 60)/2.0);
    
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 15, 0, 15);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 15;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0;
    SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];
    
    if (section == 0) {
        
        height = list.resultFeature.length > 0? 127:69;
        
    }else{
        height = list.productList.count > 0? 58:0;
    }
    return CGSizeMake(SDMainScreenWidth, height);
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)return;
    SDTestReportDeatilDetailList *deatilList = self.dataReport.detailList[self.indexPath.row];
    SDTestReportDeatilProductList *productList = deatilList.productList[indexPath.row];
    if (productList.bImg.length == 0)return;
    SDProductDetailVC *vc = [SDMethodTool getViewControlWithIdentifier:NSStringFromClass([SDProductDetailVC class])];
   
    vc.list = productList.bImg;
    [self.navigationController pushViewController:vc animated:YES];
    
}
//- (void)scrollViewDidScroll:(UIScrollView *)scrollView
//{
//
//    UIColor *colorCollection = scrollView.contentOffset.y < 127? SDClearColor:rgba(245, 245, 245, 1);
//    UIColor *colorView = scrollView.contentOffset.y < 127? rgba(245, 245, 245, 1):SDWhiteColor;
//    if (colorCollection != self.collectionView.backgroundColor)[self.collectionView setBackgroundColor:colorCollection];
//    if (colorView != self.view.backgroundColor)[self.view setBackgroundColor:colorView];
//
//}
- (void)layOut
{
    
    [self.scrHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.bottom.equalTo(self.headerView);
    }];
    
}
- (SDAnswerDetectionHeaderView *)scrHeaderView
{
    if (!_scrHeaderView) {
        
        _scrHeaderView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([SDAnswerDetectionHeaderView class]) owner:nil options:nil].lastObject;
        [_scrHeaderView setViews];
        @weakify(self);
        _scrHeaderView.block = ^(NSIndexPath *IndexPath) {
            @strongify(self);
            self.indexPath = IndexPath;
        };
        
        [self.headerView addSubview:_scrHeaderView];
        
    }
    
    return _scrHeaderView;
    
}
- (void)setIndexPath:(NSIndexPath *)indexPath
{
    _indexPath = indexPath;
   [self.collectionView reloadData];
    SDTestReportDeatilDetailList *list = self.dataReport.detailList[self.indexPath.row];
    [self getTopTitleWithStr:list.resultDesc];
    
}
- (void)getTopTitleWithStr:(NSString *)str
{
    NSString *strLast = [str componentsSeparatedByString:@"$"].lastObject;
    
    NSMutableAttributedString *att = [[NSMutableAttributedString alloc] initWithString:[str stringByReplacingOccurrencesOfString:@"$" withString:@""]];
    [att addAttribute:NSForegroundColorAttributeName value:rgba(255, 120, 155, 1) range:NSMakeRange(att.length - strLast.length, strLast.length)];
    [self.topLab setAttributedText:att];
    
}

- (IBAction)back:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
